

(defun my-abs (n)
  (if (< n 0) (- n) n)
  )
(defun half (a b)
    (/ (+ a b) 2.0))

(defun nd-sqrt (num)
  "牛顿法求平方根,对很小的数不起作用,如:0.000001"
  (defun iter (guess)
    (if (enough guess) guess (iter (newguess guess)))
    )
  (defun square (x)
    (* x x))
  ;; (defun minus (a b)
  ;;   (- a b))
  (defun enough (guess)
    (< (my-abs (- (square guess) num)) 0.0001)
    )

  (defun newguess (guess)
    (half guess (/ num guess)))

  (iter 1)
  )

(nd-sqrt 9)


(defun good-enough? (guess newguess)
    (< (my-abs (/ (- guess newguess) guess)) 0.0001))

(defun newguess (guess num)
  (half guess (/ num guess)))

(newguess 1.5 2)
  

(good-enough? 1 1.5)

(defun nd-sqrt-2 (num)
  "为了求很小的数的平方根"
  (defun iter (guess)
    (let* (ng)
      (setq ng (newguess guess num))
	(if (good-enough? guess ng) guess
	  (iter ng)
	 )
      ))
  (iter 1)
  )

(nd-sqrt-2 0.00001)

;; (* 0.0031622926477232706 0.0031622926477232706)
